package com.keyue.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keyue.common.constant.ServerResult;
import com.keyue.domain.dto.admin.AdminUserRoleDO;
import com.keyue.domain.vo.admin.AdminUserRoleQueryVO;
import com.keyue.domain.vo.admin.AdminUserRoleVO;
import com.keyue.mapper.admin.AdminUserRoleMapper;
import com.keyue.service.admin.AdminUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 用户角色表(AdminUserRole)表服务实现类
 *
 * @author makejava
 * @since 2023-08-30 17:51:49
 */
@Slf4j
@Service
public class AdminUserRoleServiceImpl extends ServiceImpl<AdminUserRoleMapper, AdminUserRoleDO> implements AdminUserRoleService {


    /**
     * 通过ID查询单条数据
     * <p>
     * adminUserRoleQueryVO 主键
     *
     * @return 实例对象
     */
    @Override
    public ServerResult queryById(AdminUserRoleQueryVO adminUserRoleQueryVO) {
        if (Objects.isNull(adminUserRoleQueryVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        AdminUserRoleDO AdminUserRoleDo = getById(adminUserRoleQueryVO.getId());
        if (Objects.isNull(AdminUserRoleDo)) {
            return ServerResult.success(null);
        }
        return ServerResult.success(BeanUtil.copyProperties(AdminUserRoleDo, AdminUserRoleVO.class));
    }

    /**
     * 分页查询
     *
     * @param adminUserRoleQueryVO 筛选条件
     * @return 查询结果
     */
    @Override
    public ServerResult list(AdminUserRoleQueryVO adminUserRoleQueryVO) {

        return ServerResult.success();
    }

    /**
     * 新增数据
     *
     * @param adminUserRoleVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult add(AdminUserRoleVO adminUserRoleVO) {
        //todo 校验
        if (Objects.nonNull(adminUserRoleVO.getId())) {
            adminUserRoleVO.setId(null);
        }
        AdminUserRoleDO adminUserRoleDo = BeanUtil.copyProperties(adminUserRoleVO, AdminUserRoleDO.class);

        save(adminUserRoleDo);

        return ServerResult.success();
    }

    /**
     * 修改数据
     *
     * @param adminUserRoleVO 实例对象
     * @return 实例对象
     */
    @Override
    public ServerResult edit(AdminUserRoleVO adminUserRoleVO) {
        //todo 校验
        if (Objects.isNull(adminUserRoleVO.getId())) {
            return ServerResult.fail("id 不能为空");
        }
        AdminUserRoleDO adminUserRoleDo = BeanUtil.copyProperties(adminUserRoleVO, AdminUserRoleDO.class);

        updateById(adminUserRoleDo);

        return ServerResult.success();
    }

    /**
     * 通过主键删除数据
     *
     * @param adminUserRoleQueryVO 筛选条件
     * @return 是否成功
     */
    @Override
    public ServerResult deleteById(AdminUserRoleQueryVO adminUserRoleQueryVO) {
        List<Long> ids = adminUserRoleQueryVO.getIds();
        if (CollUtil.isEmpty(ids)) {
            return ServerResult.fail("ids 参数不能为空");
        }
        List<AdminUserRoleDO> adminUserRoleList = listByIds(ids);
        if (CollUtil.isEmpty(adminUserRoleList)) {
            return ServerResult.success();
        }
        //todo 校验是否可以逻辑删除
        removeByIds(ids);
        return ServerResult.success();
    }
}
